<?php
/**
 * Created by PhpStorm.
 * User: g
 * Date: 2018/2/7
 * Time: 18:52
 */

namespace app\index\controller;


use app\index\validate\Valid;
use think\Request;

class Message extends Common
{
    public function index(){
        $title = $this->request->get('title');
        $this->assign('title',$title);
        $is_read = $this->request->get('is_read');
        $this->assign('is_read',$is_read);
        $start = $this->request->get('start');
        $this->assign('start',$start);
        $end = $this->request->get('end');
        $this->assign('end',$end);
        $is_read = $this->request->param('is_read');
        $this->assign('is_read',$is_read);
        $fslx = $this->request->param('fslx');
        $this->assign('fslx',$fslx);

        $where = [];
//        $where['sjr_id'] = session('employee_id');

        if($title){
            $where['title'] = ['like',"%{$title}%"];
        }
        if($fslx==1){
            $where['fsr'] = session('employee_id');
            $where['is_read'] = 1;
        }elseif($fslx==2){
            $where['fsr'] = session('employee_id');
            $where['is_read'] = 0;
        }else{
            $where['txry'] = ['like','%"'.session('employee_id').'"%'];
        }
        if(null!==$is_read&&''!==$is_read){
            $where['is_read'] = $is_read;
        }
        if($start&&!$end){
            $where['cssj'] = ['>',$start];
        }elseif($end&&!$start){
            $where['cssj'] = ['<',$end];
        }elseif($start&&$end){
            $where['cssj'] = [['>',$start],['<',$end],'and'];
        }

        $count = db('cg_message')->where($where)->count();
        $data = db('cg_message')->where($where)->order('fssj desc')->paginate(10,$count);
//        echo db('cg_message')->getLastSql();
        $page = $data->render();
        $data = $data->items();
        foreach($data as $k=>$v){
            $data[$k] = $v;
        }
        $this->assign([
            'page'=>$page,
            'data'=>$data,
        ]);
        return $this->fetch();
    }

    //标记为已读或未读
    public function mark_read(){
        $type = $this->request->param('type');
        $data = $this->request->post('data/a');
        $where = [];
        $where['id'] = ['in',$data];
        $bool = db('cg_message')->where($where)->update(['is_read'=>$type]);
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //标记全部为已读
    public function mark_read_all(){
        $where = [];
        $where['is_read'] = 0;
        $bool = db('cg_message')->where($where)->update(['is_read'=>1]);
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //删除选中
    public function delsel(){
        $data = $this->request->post('data/a');
        $where = [];
        $where['id'] = ['in',$data];
        $bool = db('cg_message')->where($where)->delete();
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }
    //删除选中
    public function delete(){
        $id = $this->request->post('id');
        $where = [];
        $where['id'] = $id;
        $bool = db('cg_message')->where($where)->delete();
        if(false !== $bool){
            echo json_encode(['success'=>true,'msg'=>'ok']);
        }else{
            echo json_encode(['success'=>false,'msg'=>'error']);
        }
    }

    //详情页
    public function view(){
        $id = $this->request->param('id');
        $data = db('cg_message')->where('id',$id)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }

    //新增
    public function add(){
        if($_POST){
            $data = Request::instance()->post();

            $valid = new Valid([
                'title|主题'=>'require',
                'content|内容'=>'require',
                'txrq|提醒日期'=>"require",
                'txfs|提醒方式'=>'require',
            ]);
            if(!$valid->check($data)){
                echo json_encode(['success'=>false,'msg'=>$valid->getError()]);die;
            }
            if(!isset($data['txry'])||!is_array($data['txry'])||count($data['txry'])==0){
                echo json_encode(['success'=>false,'msg'=>'请选择提醒人员']);die;
            }
            $data['txry'] = serialize($data['txry']);
            $data['fssj'] = date('Y-m-d H:i:s');
            $data['fsr'] = session('employee_id');
            $data['fsr_name'] = db('employee')->where('id',session('employee_id'))->value('em_name');
            $bool = db('cg_message')->insert($data);
            if(false !== $bool){
                echo json_encode(['success'=>true,'msg'=>'ok']);
            }else{
                echo json_encode(['success'=>false,'msg'=>'error']);
            }
            die;
        }
        $employee_id = session('employee_id');
        $em_name = db('employee')->where('id',$employee_id)->value('em_name');
        $this->assign('em_name',$em_name);
        $this->assign('employee_id',$employee_id);
        return $this->fetch();
    }
}